package com.app.util;

import java.math.BigDecimal;

public class AmountConverter {

    private static final String[] NUMBERS = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
    private static final String[] UNITS = {"", "拾", "佰", "仟"};
    private static final String[] BIG_UNITS = {"", "万", "亿", "兆"};
    private static final String DECIMAL_UNITS[] = {"角", "分"};

    public static String convertToChinese(BigDecimal amount) {
        if (amount == null) {
            return "";
        }
        amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP);
        String numStr = amount.abs().toPlainString();
        String[] parts = numStr.split("\\.");
        String integerPart = parts[0];
        String decimalPart = parts.length > 1 ? parts[1] : "00";

        StringBuilder result = new StringBuilder();
        if ("0".equals(integerPart)) {
            result.append("零");
        } else {
            int groupCount = (integerPart.length() + 3) / 4;
            int padding = groupCount * 4 - integerPart.length();
            // 使用 StringBuilder 手动重复 "0"
            StringBuilder paddingStr = new StringBuilder();
            for (int i = 0; i < padding; i++) {
                paddingStr.append("0");
            }
            integerPart = paddingStr.toString() + integerPart;

            for (int i = 0; i < groupCount; i++) {
                String group = integerPart.substring(i * 4, (i + 1) * 4);
                String groupChinese = convertGroup(group);
                if (!"".equals(groupChinese)) {
                    result.append(groupChinese).append(BIG_UNITS[groupCount - i - 1]);
                }
            }
            // 去除多余的零
            result = new StringBuilder(result.toString().replaceAll("零+", "零").replaceAll("零([万亿兆])", "$1").replaceAll("零$", ""));
        }

        if (!"00".equals(decimalPart)) {
            if (result.length() > 0) {
                result.append("元");
            }
            for (int i = 0; i < decimalPart.length(); i++) {
                int digit = decimalPart.charAt(i) - '0';
                if (digit != 0) {
                    result.append(NUMBERS[digit]).append(DECIMAL_UNITS[i]);
                }
            }
        } else {
            if (result.length() > 0) {
                result.append("元整");
            }
        }

        if (amount.compareTo(BigDecimal.ZERO) < 0) {
            result.insert(0, "负");
        }

        return result.toString();
    }

    private static String convertGroup(String group) {
        StringBuilder groupResult = new StringBuilder();
        boolean zeroFlag = false;
        for (int i = 0; i < group.length(); i++) {
            int digit = group.charAt(i) - '0';
            if (digit == 0) {
                zeroFlag = true;
            } else {
                if (zeroFlag) {
                    groupResult.append("零");
                    zeroFlag = false;
                }
                groupResult.append(NUMBERS[digit]).append(UNITS[3 - i]);
            }
        }
        return groupResult.toString().replaceAll("零+$", "");
    }

    public static void main(String[] args) {
        BigDecimal actualAmount = new BigDecimal("3283.21");
        String chineseAmount = convertToChinese(actualAmount);
        System.out.println(chineseAmount);
    }
}
